﻿using System;
using System.Drawing;
using System.Net.NetworkInformation;
using System.Windows.Forms;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;
using ZXing.QrCode.Internal;

namespace 二维码解析和生成
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 选择二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PictureBox_Qrcode_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.ShowDialog();

            if (dialog.FileName.Length != 0)
            {
                try
                {
                    this.pictureBox_Qrcode.Image = new Bitmap(dialog.FileName);
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }

        /// <summary>
        /// 生成文本的二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Create_Click(object sender, EventArgs e)
        {
            string content = this.textBox_Content.Text;
            if (content.Length == 0)
            {
                MessageBox.Show("空字符串无效");
                return;
            }

            try
            {
                BarcodeWriter writer = new BarcodeWriter
                {
                    Format = BarcodeFormat.QR_CODE,
                    Options = new QrCodeEncodingOptions()
                    {
                        DisableECI = true,
                        //设置内容编码
                        CharacterSet = "UTF-8",
                        //设置二维码的宽度和高度
                        Width = 300,
                        Height = 300,
                        //设置二维码的边距,单位不是固定像素
                        Margin = 1
                    }
                };

                Bitmap bitmap = writer.Write(content);

                if (this.pictureBox_Qrcode.Image != null)
                {
                    this.pictureBox_Qrcode.Image.Dispose();
                }
                this.pictureBox_Qrcode.Image = bitmap;
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        /// <summary>
        /// 解析二维码生成文本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Solve_Click(object sender, EventArgs e)
        {
            try
            {
                IBarcodeReader reader = new BarcodeReader();
                Bitmap bmp = new Bitmap(this.pictureBox_Qrcode.Image);
                Result result = reader.Decode(bmp);
                bmp.Dispose();
                if (result != null)
                {
                    //条码内容
                    string text = result.Text;
                    this.textBox_Content.Text = text;
                    //条码类型
                    //string foramt = result.BarcodeFormat.ToString(); 
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }

        /// <summary>
        /// 保存生成的二维码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Save_Click(object sender, EventArgs e)
        {
            if (this.pictureBox_Qrcode.Image == null)
            {
                MessageBox.Show("暂未生成二维码图片，无法保存");
                return;
            }

            SaveFileDialog dialog = new SaveFileDialog();
            dialog.DefaultExt = "*.png";
            string defaultName = DateTime.Now.ToString("yyyyMMdd_HHmmssfff");
            dialog.Filter = $"{defaultName}|*.jpg|{defaultName}|*.jpeg|{defaultName}|*.png";
            dialog.ShowDialog();

            try
            {
                if (dialog.FileName.Length > 0)
                {
                    this.pictureBox_Qrcode.Image.Save(dialog.FileName);
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
    }
}
